package com.jiayang.hhotel.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.bean.RoomNumModel;
import com.jiayang.hhotel.util.StringUtil;
import com.jiayang.hhotel.util.ToastUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Random;

/**
 * Created by Administrator on 2017/5/24 0024.
 * 订房查询详情页面分房对话框中的grid view适配器
 */

public class DivideAdapter extends BaseAdapter{

    private Context t;
    private List<RoomNumModel.PRsvforcastRoomDtoListBean> mList;
    private int maxCount;//可选择的最大房数
    private HashMap<Integer,Boolean> isCheck;

    public DivideAdapter(Context context, List<RoomNumModel.PRsvforcastRoomDtoListBean> list, int maxCount){
        this.t=context;
        this.mList=list;
        this.maxCount=maxCount;
        initHashMap();
    }

    public void initHashMap(){
        if(isCheck ==null){
            isCheck =new HashMap<>();
        }
        for(int i=0;i<mList.size();i++){
            isCheck.put(i,false);
        }
    }
    //获取已选择的房间号拼接
    public String getRoomList(){
        String room="";
        for(int i=0;i<isCheck.size();i++){
            if(isCheck.get(i)){
                room+=mList.get(i).getRoom()+",";
            }
        }
        if(!StringUtil.isEmpty(room)){
            room=room.substring(0,room.length()-1);
        }
        return room;
    }

    //获取已选择房间数量
    public int getSelectRoomCount(){
        int count=0;
        for(int i=0;i<isCheck.size();i++){
            if(isCheck.get(i)){
                count++;
            }
        }
        return count;
    }

    private Random random;
    /**
     * 设置随机房号
     */
    public void randomRoom(){
        if(getSelectRoomCount() < maxCount){//选中的房间数量小于可选最大房间数量
            if(random == null){
                random = new Random();
            }
            int index;
            for (int i = 0;i < maxCount - getSelectRoomCount();i++){
                index = random.nextInt(getCount());
                while (isCheck.get(index)){//若随机到的下标是已经被选中的
                    index = random.nextInt(getCount());
                }
                isCheck.put(index,true);
            }
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    ViewHolder holder;
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        RoomNumModel.PRsvforcastRoomDtoListBean room=mList.get(position);
        if(convertView==null){
            convertView= LayoutInflater.from(t).inflate(R.layout.divide_gridview_item,null);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder= (ViewHolder) convertView.getTag();
        holder.item.setText(room.getRoom());
        if(isCheck.get(position)){
            holder.item.setChecked(true);
        }else{
            holder.item.setChecked(false);
        }
        holder.item.setOnClickListener(new Listener(position));
        return convertView;
    }

    public class ViewHolder{
        CheckBox item;

        public ViewHolder(View view){
            item= (CheckBox) view.findViewById(R.id.cb);
        }
    }

    class Listener implements View.OnClickListener{

        private int position;
        public Listener(int position){
            this.position=position;
        }

        @Override
        public void onClick(View v) {
            if(getSelectRoomCount() < maxCount){
                if(isCheck.get(position)){
                    isCheck.put(position,false);
                }else{
                    isCheck.put(position,true);
                }
            }else{
                if(isCheck.get(position)){
                    isCheck.put(position,false);
                }else {
                    ((CheckBox) v).setChecked(false);
                    ToastUtil.Toast(t.getString(R.string.maximum_bookings_have_been_reached), t);
                }
            }
        }
    }

}
